package com.nurese.service;

import com.github.pagehelper.PageHelper;
import com.nurese.dao.UserDao;
import com.nurese.entity.User;
import com.nurese.utils.BeanMapUtils;
import com.nurese.utils.MapParameter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserService {
    @Autowired
    private UserDao userDao;

    public int create(User user){
        System.out.println("++++2:"+BeanMapUtils.beanToMap(user));
        return userDao.create(user);
    }
    public int delete(Integer id){
//        Map<String,Object> map=new HashMap<>();
//        map.put("id",id);
//        return userDao.delete(map);

        return userDao.delete(MapParameter.getInstance().add("id",id).getMap());
    }

    public int delete(String ids){

        String[] arr=ids.split(",");
        int flag=0;
        for (String s:arr){
            flag=userDao.delete(MapParameter.getInstance().addId(Integer.parseInt(s)).getMap());
        }
        return flag;
    }
    public int update(User user){
//        Map<String,Object> map= BeanMapUtils.beanToMapForUpdate(user);
//        return userDao.update(map);

          return userDao.update(MapParameter.getInstance()
                  .add(BeanMapUtils.beanToMapForUpdate(user))
                  .addId(user.getId()).getMap());
    }
    public List<User> query(User user){
        System.out.println("++++1:"+BeanMapUtils.beanToMap(user));
        if (user !=null && user.getPage() !=null){
            PageHelper.startPage(user.getPage(),user.getLimit());
        }
        return userDao.query(BeanMapUtils.beanToMap(user));
    }
    public int count(User user){
        return userDao.count(BeanMapUtils.beanToMap(user));
    }
    public User detail(Integer id){
        return userDao.detail(MapParameter.getInstance().add("id",id).getMap());
    }
}
